package thread;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

// 单例模式
class SingleLazy { //懒汉模式
    private static volatile SingleLazy singleLazy = null;


    public static SingleLazy getSingleLazy() {
        if (singleLazy == null) {
            synchronized(SingleLazy.class) {
                if (singleLazy == null) {
                    singleLazy = new SingleLazy();
                }
            }
        }
        return singleLazy;
    }

    private SingleLazy() {

    }
}

class SingleHunger { // 饿汉模式
    int num = 0;
    private  static SingleHunger singleHunger = new SingleHunger(1);
    public SingleHunger getSingleHunger() {
        return singleHunger;
    }
    private SingleHunger(int num) {
        this.num = num;

    }

    @Override
    public String toString() {
        return "SingleHunger{" +
                "num=" + num +
                '}';
    }
}
 public class demo5 {
     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//         SingleLazy instance = new SingleLazy();
         Class<?> singleLazy = SingleLazy.class;
         Class<?> singleLazy2 = null;
         singleLazy2 = Class.forName("thread.SingleHunger");
         Constructor<?> declaredConstructor = singleLazy2.getDeclaredConstructor(int.class);
         declaredConstructor.setAccessible(true);
         Object o = declaredConstructor.newInstance(2);
         SingleHunger singleLazyNew = (SingleHunger) o;
         System.out.println(singleLazyNew);


     }
}
